<template>
	<el-dialog
		:title="isMember ? '修改会员返水比例' : '修改代理收益比例'"
		center
		:visible.sync="visible"
		width="1200px"
		class="auit-confirm"
		:before-close="handleCancel"
	>
		<div class="commissionsDialog">
			<AgentRebates
				ref="AgentRebates"
				labelWidth="160px"
				memberPlatformLabel="平台发放返水比例:"
				:queryRebateRateParams="queryRebateRateParams"
				:agentData="agentData"
				:isShow="isShow"
				:isEdit="true"
				:isMember="row.userType === 0"
				@agentPointSetAllSubOk="agentPointSetAllSubOk"
			/>
		</div>
		<div slot="footer" class="dialog-footer">
			<template v-if="isEdit">
				<el-button @click="handleCancel">
					{{ $t('agent.cancel') }}
				</el-button>
				<el-button type="primary" :loading="loading" @click="handleOK">
					{{ $t('agent.submit') }}
				</el-button>
			</template>
		</div>
	</el-dialog>
</template>
<script>
import AgentRebates from '@/components/AgentRebates'
export default {
	name: 'CommissionsDialog',
	components: {
		AgentRebates
	},
	props: {
		visible: {
			type: Boolean,
			default: false
		},
		isEdit: {
			type: Boolean,
			default: false
		},
		row: {
			type: Object,
			default: () => ({})
		},
		// eslint-disable-next-line vue/require-default-prop, vue/require-prop-types
		refresh: {
			typeof: Function
		},
		teamAgent: {
			type: Object,
			default: () => ({})
		}
	},
	data() {
		return {
			loading: false,
			modal: true,
			memberPlatformConfig: {
				query: {
					platformFlag: 1,
					userType: 0,
					zhanchengFlag: 0,
					status: 1
				}
			}
		}
	},
	computed: {
		// 是否是会员返水
		isMember() {
			return this.row.userType === 0
		},
		isShow() {
			if (this.isMember) {
				return ['memberPlatform', 'agentPoint']
			}
			return ['dzRebates', 'teamRebates', 'teamContribution']
		},
		queryRebateRateParams() {
			return {
				merchantId: this.row.merchantId,
				userName: this.row.userName,
				userId: this.row.userId,
				status: this.row.status === 0 ? undefined : this.row.status,
				userType: this.row.userType
				// version: this.row.version
			}
		},
		agentData() {
			return Object.assign({}, this.row, {
				id: this.row.userId
			})
		}
	},
	created() {
		// this.$refs['AgentRebates'].queryRebateRate(this.queryRebateRateParams)
	},
	methods: {
		handleCancel() {
			this.$emit('update:visible', false)
			this.$refs['AgentRebates'].resetFields()
			this.$refs['AgentRebates'].agentPointSetAllSubVisible = false
		},
		async handleOK() {
			try {
				await this.$refs['AgentRebates'].validate()
				const tableNames = this.isMember
					? ['memberPlatform']
					: ['teamRebates', 'teamContribution']
				const rebateRateList = this.$refs['AgentRebates'].getValues(
					tableNames
				)

				const params = {
					userId: this.row.userId,
					merchantId: this.row.merchantId,
					userType: this.row.userType,
					status: 1,
					rebateRateList
				}

				if (this.isMember) {
					await this.$confirm(
						`确认修改返水比例后，针对待结算返点账单即刻生效。修改前返水比例将生成已失效记录。`,
						'确认提示'
					)
					const ret = await this.$api.rebateRateUdate(params)
					if (ret.code === 200) {
						this.$message.success(this.$t('common.save_success'))
						this.handleCancel()
					}
					this.refresh()
				} else {
					// this.$emit('update:visible', false)
					this.modal = false
					setTimeout(() => {
						this.$refs[
							'AgentRebates'
						].agentPointSetAllSubVisible = true
					}, 100)
				}
			} catch (err) {
				console.error(err)
				throw err
			}
		},
		async agentPointSetAllSubOk({ adjustmentType }) {
			const params = {
				adjustmentType,
				commissionMode: 1,
				rebateAllotMode: 1,
				isAllSub: this.isAllSub,
				topProxyId: this.agentData.topProxyId,
				userId: this.agentData.topProxyId,
				userType: 1,
				rebateRateList: this.$refs['AgentRebates'].getValues([
					'teamRebates',
					'teamContribution'
				])
			}

			const agentRebatesRef = this.$refs['AgentRebates']
			try {
				agentRebatesRef.agentPointSetAllSubLoading = true
				await agentRebatesRef.upadteRebateRate(params)
				// 弹出修改返点信息弹框
				agentRebatesRef.agentPointSetAllSubVisible = false
				this.$emit('update:visible', false)
				this.$message.success(this.$t('common.save_success'))
				this.refresh()
			} catch (err) {
				throw err
			} finally {
				agentRebatesRef.agentPointSetAllSubLoading = false
			}
		}
	}
}
</script>

<style scoped>
/* ::v-deep .commissionsDialog .point-box {
	width: 1150px !important;
} */
/* ::v-deep .commissionsDialog .pointTable {
        display: none;
    } */
</style>
